package com.text;

import com.dao.user.ProviderMapper;
import com.entity.Bill;
import com.entity.Provider;
import com.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PorviderTest {
    private Logger logger =Logger.getLogger(UserMapperTest.class);
    @Test
    public void testAddProvider(){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            Provider provider = new Provider();
            provider.setProCode("ZJ_GYS003");
            provider.setProName("乐摆日用品厂");
            provider.setProDesc("长期合作伙伴，主营产品：各种中、高档塑料杯，塑料乐扣水杯（密封杯）、保鲜杯（保鲜盒）、广告杯、礼品杯");
            provider.setProContact("小明");
            provider.setProPhone("12312345678");
            provider.setProAddress("浙江省金华市义乌市义东路");
            provider.setProFax("0579-34452321");
            provider.setCreatedBy(1);
            provider.setCreationDate(new Date(1111-11-11));
            count = sqlSession.getMapper(ProviderMapper.class).addProvider(provider);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug(count);
    }

    @Test
    public void tesuUpdateProvider(){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            Provider provider = new Provider();
            provider.setProCode("12344");
            provider.setProName("123");
            provider.setProDesc("123");
            provider.setProContact("123");
            provider.setProPhone("12312312123");
            provider.setProAddress("123-123");
            provider.setProFax("123-123");
            provider.setCreatedBy(null);
            provider.setCreationDate(new java.util.Date());
            provider.setId(17);
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(ProviderMapper.class).updateProvider(provider);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug(count);
    }

    @Test
    public void testProviderId(){
        SqlSession sqlSession = null;
        List<Provider> providerList = new ArrayList<Provider>();
        Integer id = 3;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            providerList = sqlSession.getMapper(ProviderMapper.class).selectProviderId(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Provider provider : providerList){
            logger.debug("供应商id" + provider.getId() + "供应商编码" + provider.getProCode()
                    + "供应商名称" + provider.getProName() + "供应商联系人" + provider.getProContact()
                    + "供应商联系电话" + provider.getProPhone());
            for (Bill bill : provider.getBillList()){
                logger.debug("订单编码" + bill.getBillCode() + "商品名称" + bill.getProductName()
                        + "订单金额" + bill.getTotalPrice() + "是否付款" + bill.getIsPayment());
            }
        }
    }
    @Test
    public void testDeleteProvider(){
        SqlSession sqlSession = null;
        int id = 16;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(ProviderMapper.class).deleteProvider(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count= 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug(count);
    }
    @Test
    public void testProviderByRoleId_foreach_array(){
        SqlSession sqlSession = null;
        List<Provider> providerList = new ArrayList<Provider>();
        Integer[] idArray = {1,3};
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            providerList = sqlSession.getMapper(ProviderMapper.class).getProviderById_foreach_array(idArray);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Provider provider : providerList) {
            logger.debug("供应商编码" + provider.getProCode() + "供应商名称" + provider.getProName());
        }
    }

    @Test
    public void testProviderByRoleId_foreach_list() {
        SqlSession sqlSession = null;
        List<Provider> providerList = new ArrayList<Provider>();
        List<Integer> idList = new ArrayList<Integer>();
        idList.add(1);
        idList.add(3);
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            providerList = sqlSession.getMapper(ProviderMapper.class).getProviderById_foreach_list(idList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Provider provider : providerList) {
            logger.debug("供应商编码" + provider.getProCode() + "供应商名称" + provider.getProName());
        }
    }

    @Test
    public void testProviderList_choose(){
        SqlSession sqlSession = null;
        List<Provider> providerList = new ArrayList<Provider>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            providerList = sqlSession.getMapper(ProviderMapper.class).getProviderList_choose("001","深圳","邓","2019-01-01");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Provider provider : providerList) {
            logger.debug("供应商编码" + provider.getProCode() + "供应商名称" + provider.getProName());
        }
    }
}
